package com.example.wasaimovie.Controller;

import com.example.wasaimovie.Annotation.DisableBaseResponse;
import com.example.wasaimovie.Model.entity.Cart;
import com.example.wasaimovie.Model.support.ResponseResult;
import com.example.wasaimovie.Service.ArrangementService;
import com.example.wasaimovie.Service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api")
@Slf4j
public class CartController {

    @Autowired
    CartService cartService;

    @Autowired
    ArrangementService arrangementService;

    /**
     * 加入购物车
     * @param cart 购票信息
     * @return 是否成功加入
     */
    @CrossOrigin
    @PostMapping("/cart")
    @DisableBaseResponse
    public ResponseResult<Object> getCart(@RequestBody Cart cart){
        log.info("CartController -> getCart");
        cart.setId(UUID.randomUUID().toString());
        cart.setCreateTime(new Date());
        cartService.save(cart);
        return new ResponseResult<>(200);
    }

    /**
     * 查询某个用户购物车信息
     * @param uid 用户id
     * @return 购物车信息
     */
    @CrossOrigin
    @GetMapping("/cart/{uid}")
    public List<Map<String, Object>> getCartById(@PathVariable String uid){
        log.info("CartController -> getCartById");
        List<Map<String, Object>> msgByCart = cartService.getMsgByCart(uid);
        return msgByCart;
    }

    /**
     * 删除购物车
     * @param id 记录id
     * @return 购物信息
     */
    @CrossOrigin
    @DeleteMapping("/cart")
    @DisableBaseResponse
    public ResponseResult<Object> deleteCart(@RequestBody String id){
        log.info("CartController -> deleteCart");
        return cartService.deleteCartById(id);
    }
}
